Page({
  data: {
    chatList: [
      {
        id: 1,
        name: "张同学",
        avatar: "/images/icons/avatar.png",
        itemName: "华硕笔记本电脑",
        lastMessage: "你好，我在图书馆看到了类似的电脑",
        time: "14:30",
        unreadCount: 2,
        messages: [
          { type: "received", content: "你好，请问是你丢了华硕笔记本电脑嘛", time: "14:20" },
          { type: "sent", content: "是的！你有看到咩", time: "14:22" },
          { type: "received", content: "我在图书馆三楼看到了类似的电脑", time: "14:25" },
          { type: "received", content: "你可以过来确认一下", time: "14:25" }
        ]
      },
      {
        id: 2,
        name: "李老师",
        avatar: "/images/icons/avatar.png",
        itemName: "AirPods耳机",
        lastMessage: "同学，我在教室捡到了耳机",
        time: "昨天",
        unreadCount: 0,
        messages: [
          { type: "received", content: "同学你好，我在A402教室捡到了一个耳机", time: "昨天 16:30" },
          { type: "sent", content: "是AirPods嘛", time: "昨天 16:35" },
          { type: "sent", content: "是滴", time: "昨天 16:35" },
          { type: "received", content: "明天来教务处领取吧", time: "昨天 16:40" }
        ]
      },
      {
        id: 3,
        name: "王同学",
        avatar: "/images/icons/avatar.png",
        itemName: "学生证",
        lastMessage: "我好像捡到了你的学生证",
        time: "周三",
        unreadCount: 1,
        messages: [
          { type: "received", content: "你好，我捡到了一张学生证", time: "周三 12:30" },
          { type: "sent", content: "!!!能描述一下样子嘛", time: "周三 12:35" },
          { type: "received", content: "照片是个戴眼镜的男生，红色头发", time: "周三 12:40" }
        ]
      }
    ]
  },

  onLoad: function() {
    // 加载聊天列表数据
  },

  onShow: function() {
    // 页面显示时刷新未读消息数
    this.updateUnreadCount();
  },

  navigateToChat: function(e) {
    const chatData = e.currentTarget.dataset.chat;
    
    // 清空该聊天的未读消息数
    const chatId = chatData.id;
    this.clearUnreadCount(chatId);
    
    // 跳转到聊天页面
    wx.navigateTo({
      url: `/packageDetail/pages/chat/chat?chatData=${encodeURIComponent(JSON.stringify(chatData))}`
    });
  },

  clearUnreadCount: function(chatId) {
    const chatList = this.data.chatList.map(chat => {
      if (chat.id === chatId) {
        return { ...chat, unreadCount: 0 };
      }
      return chat;
    });
    
    this.setData({ chatList });
  },

  updateUnreadCount: function() {
    // 这里可以从服务器获取最新的未读消息数
    console.log("更新未读消息数");
  }
});